<?
function drizzo_get_the_trash($id = 0) {
	drizzo_db_connect;

	global $drizzo_trashheap;
	
	$trash_post = @mysql_query("SELECT * FROM `$drizzo_trashheap` WHERE `ID`='$id' LIMIT 1");
	if(mysql_errno()) {
		return false;
	} else {
		while($row = @mysql_fetch_assoc($trash_post)) {
			$author_meta = drizzo_user_info($row['author']);
			if($author_meta['nice_name']) {
				$author = $author_meta['nice_name'];
			} else {
				$author = $author_meta['username'];
			}
			if(drizzo_global_options('time_format')) {
				$time = drizzo_global_options('time_format');
			} else {
				$time = "M d, Y \\a\\t g:i A";
			}
			foreach($row as $field => $value) {
				if($field != 'posted_date' && $field != 'edited_date' && !is_int($field)) {
					$the_post[$field] = drizzo_db_unwrap($value);
				}
			}
			$the_post['author_nicename'] = $author;
			$the_post['comment_count'] = drizzo_comment_count($row['old_id']);
			$the_post['posted_date'] = date($time, $row['posted_date']);
			$the_post['posted_date_raw'] = $row['posted_date'];
			$the_post['edited_date'] = date($time, $row['edited_date']);
			$the_post['edited_date_raw'] = $row['edited_date'];
		}
		return $the_post;
	}
}

function drizzo_get_the_trashheap($limit = 20, $offset = 0) {
	global $drizzo_trashheap, $drizzo_comments, $drizzo_users;
	
	$types = $type_string;
	
	drizzo_db_connect();

	$the_posts_query = @mysql_query("SELECT * FROM `$drizzo_trashheap` ORDER BY `ID` DESC LIMIT $offset,$limit");

	if(mysql_errno()) {
		$the_posts[] = "<p>Posts could not be retrieved.</p>";
	} else {
		while($row = @mysql_fetch_assoc($the_posts_query)) {
			$author_meta = drizzo_user_info($row['author']);
			if($author_meta['nice_name']) {
				$author = $author_meta['nice_name'];
			} else {
				$author = $author_meta['username'];
			}
			if(drizzo_global_options('time_format')) {
				$time = drizzo_global_options('time_format');
			} else {
				$time = "M d, Y \\a\\t g:i A";
			}
			foreach($row as $field => $value) {
				if($field != 'posted_date' && $field != 'edited_date') {
					$the_posts[$i][$field] = drizzo_db_unwrap($value);
				}
			}
			$the_posts[$i]['author_nicename'] = $author;
			$the_posts[$i]['comment_count'] = drizzo_comment_count($row['old_id']);
			$the_posts[$i]['posted_date'] = date($time, $row['posted_date']);
			$the_posts[$i]['posted_date_raw'] = $row['posted_date'];
			$the_posts[$i]['edited_date'] = date($time, $row['edited_date']);
			$the_posts[$i]['edited_date_raw'] = $row['edited_date'];
			$i++;
		}
	}
	return $the_posts;
}

function drizzo_move_to_trashheap($id) {
	global $drizzo_trashheap, $drizzo_posts;
	$delete_post = drizzo_get_the_post($id);
	extract($delete_post);
	$deleter = session_user_id;

		$trashheap = @mysql_query("INSERT INTO `$drizzo_trashheap` SET `author`='$author',`type`='$type',`status`='$status',`page_parent`='$page_parent',`page_order`='$page_order',`title`='$title',`content`='$content',`tags`='$tags',`comment_status`='$comment_status',`edited_by`='$edited_by',`edited_date`='$edited_date',`posted_date`='$posted_date',`permalink_name`='$permalink_name',`menu_name`='$menu_name',`lat`='$lat',`lon`='$lon',`old_id`='$ID',`deleted_date`=" . time() . ",`deleted_by`='$deleter'");

	if(!mysql_errno()) {
		@mysql_query("DELETE FROM `$drizzo_posts` WHERE `ID`='$id'");
		return true;
	} else {
		return false;
	}
}

function drizzo_trash_count() {
	global $drizzo_trashheap;
	$trash_count = 0;
	
	$trash_number = @mysql_query("SELECT * FROM `$drizzo_trashheap` ORDER BY `deleted_date`");
	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($trash_number)) {
			$deleted_date = $row['deleted_date'];
			$the_post['author'] = $row['author'];
			if(drizzo_post_permissions_check()) {
				$trash_count++;
			}
		}
	} elseif($trash_number && $delete && isset($_POST['delete_check'])) {
		$trash_count = $trash_count - 1;
	} else {
		$trash_count = "0";
	}
	return $trash_count;
}
?>
